package com.le.tester.leetcode;

import java.util.*;

/**
 * createTime：2022/12/23 14:31
 * description：分发糖果
 */
public class DistributeCandies {


    public int[] distributeCandies(int candies, int num_people) {

        List<Integer> result = new ArrayList<>(num_people);
        List<Integer> addMax = new ArrayList<>();
        for (int i = 0; i < num_people; i++) {
            result.add(0);
        }

        while (candies > 0) {
            //多出来的糖果,继续分
            //取得最大值
            Integer max = Collections.max(result);
            if (addMax.size() > 0) {
                max = Collections.max(addMax);
            }
            for (int i = 0; i < num_people; i++) {
                if (candies > 0) {
                    if (candies <= max + i) {
                        result.set(i, result.get(i) + candies);
                    } else {
                        result.set(i, result.get(i) + max + i + 1);
                    }
                } else {
                    break;
                }
                addMax.add(max + i + 1);
                candies = candies - (max + i + 1);
            }
        }

        return result.stream().mapToInt(item -> item).toArray();

    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new DistributeCandies().distributeCandies(600, 40)));
    }
}
